package com.study.configuration;

import com.study.filter.XssFilter;
import com.study.servlet.MyServlet;
import java.util.Arrays;
import javax.servlet.Servlet;
import javax.servlet.annotation.WebFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @author jxwen
 * @create 2018-10-19 9:14
 * @desc
 **/
@Configuration
public class XSSFilterConfig {

  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(xssFilter());
    registration.addUrlPatterns("/*");
    registration.addInitParameter("paramName", "paramValue");
    registration.setName("xssFilter");
    registration.setAsyncSupported(true);
    registration.setUrlPatterns(Arrays.asList("/*"));
    return registration;
  }

  /**
   * 创建一个bean
   *
   * @return
   */
  @Bean(name = "xssFilter")
  public Filter xssFilter() {
    return new XssFilter();
  }


  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    ServletRegistrationBean registration = new ServletRegistrationBean(myServlet(), "/servlet");
    registration.addInitParameter("paramName", "paramValue");
    registration.setName("myServlet");
    registration.setAsyncSupported(true);
    return registration;
  }

  /**
   * 创建一个bean
   *
   * @return
   */
  @Bean(name = "myServlet")
  public Servlet myServlet() {
    return new MyServlet();
  }

}
